# coding=utf-8
from django.contrib import admin
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _

import reversion
from django.db import models
from core.widget import MoneyWidget
from core.models import Package, Contract, State, City, NaturalPerson, Company, Menu, MainMenu, Action
from core.forms import NaturalPersonForm, UserForm, ContactForm, AddressForm, CompanyForm, PackageForm, ContractForm
from gym.models import MuscleGroup, Exercise, Question, Activity


class NaturalPersonAdmin(reversion.VersionAdmin):
    fieldsets = [
        (_('Main Information'), {'fields': ('name', 'cpf', 'birth_date',
                                              'civil_state', 'blood_group', 'rh_factor', 'sex', 'gym')}),
        (_('Photo'), {'fields': ['photo']}),
        (_('Contract Information'), {'fields': ['user']}),
        (_('Address Information'), {'fields': ['address']}),
        (_('Contact Information'), {'fields': ['contact']}),
    ]
    list_display = ('name', 'cpf', )
    search_fields = ('name', 'cpf')

    class Media:
        js = ('admin/js/jquery.js', 'admin/js/maskedinput.js', 'admin/js/widget.js', )

    form = NaturalPersonForm

    def render_change_form(self, request, context, *args, **kwargs):
        is_post = request.method == 'POST'
        person = context.get('original')

        user_form = UserForm(request.POST) if is_post else UserForm()
        address_form = AddressForm(request.POST) if is_post else AddressForm()
        contact_form = ContactForm(request.POST) if is_post else ContactForm()

        if person and not is_post:
            user_form = UserForm(instance=person.user)
            address_form = AddressForm(instance=person.address)
            contact_form = ContactForm(instance=person.contact)

        extra = {
            'includes': {
                'address': {'form': address_form, 'template': 'admin/core/address/address.html'},
                'contact': {'form': contact_form, 'template': 'admin/core/contact/contact.html'},
                'user': {'form': user_form, 'template': 'admin/core/user/user.html'},
            }
        }

        context.update(extra)
        return super(NaturalPersonAdmin, self).render_change_form(request, context, *args, **kwargs)


class CompanyAdmin(reversion.VersionAdmin):
    fieldsets = [
        (_('Main Information'), {'fields': ('name', 'cnpj', 'social_reason',)}),
        (_('Photo'), {'fields': ['photo']}),
        (_('Contract Information'), {'fields': ['user']}),
        (_('Address Information'), {'fields': ['address']}),
        (_('Contact Information'), {'fields': ['contact']}),
    ]

    list_display = ('name', 'cnpj', 'social_reason', )
    search_fields = ('name', 'cnpj', )

    form = CompanyForm

    class Media:
        js = ('admin/js/jquery.js', 'admin/js/maskedinput.js', 'admin/js/widget.js', )

    def render_change_form(self, request, context, *args, **kwargs):
        is_post = request.method == 'POST'
        company = context.get('original')

        user_form = UserForm(request.POST) if is_post else UserForm()
        address_form = AddressForm(request.POST) if is_post else AddressForm()
        contact_form = ContactForm(request.POST) if is_post else ContactForm()

        if company and not is_post: #change
            user_form = UserForm(instance=company.user)
            address_form = AddressForm(instance=company.address)
            contact_form = ContactForm(instance=company.contact)

        extra = {
            'includes': {
                'address': {'form': address_form, 'template': 'admin/core/address/address.html'},
                'contact': {'form': contact_form, 'template': 'admin/core/contact/contact.html'},
                'user': {'form': user_form, 'template': 'admin/core/user/user.html'},
            }
        }

        context.update(extra)
        return super(CompanyAdmin, self).render_change_form(request, context, *args, **kwargs)


class PackageAdmin(reversion.VersionAdmin):
    form = PackageForm

    class Media:
        js = ('admin/js/jquery.js', 'admin/js/maskmoney.js', 'admin/js/widget.js', )

    fieldsets = [
        (_('Main Information'), {'fields': ('active', 'name', 'price')}),
        (_('Permissions'), {'fields': ['permissions']}),
        (_('Description'), {'fields': ['description']})
    ]
    formfield_overrides = {
        models.DecimalField: {'widget': MoneyWidget}
    }
    list_display = ('name', 'price', 'active')
    list_filter = ('active', )
    search_fields = ('name', )
    filter_horizontal = ('permissions', )


class ContractAdmin(reversion.VersionAdmin):
    form = ContractForm

    fieldsets = [
        (_('Main Information'), {'fields': ('active', 'package', 'expiration')}),
        (_('Attachment'), {'fields': ['attachment']}),
        (_('Observation'), {'fields': ['observation']})
    ]
    list_display = ('id', 'package', 'active')
    list_filter = ('package', 'active')
    search_fields = ('id', )
    raw_id_fields = ('package',)


class MenuAdmin(reversion.VersionAdmin):
    pass


class MainMenuAdmin(reversion.VersionAdmin):
    pass


class CityAdmin(reversion.VersionAdmin):
    pass


class StateAdmin(reversion.VersionAdmin):
    pass


class MuscleGroupAdmin(reversion.VersionAdmin):
    pass


class ExerciseAdmin(reversion.VersionAdmin):
    pass


class QuestionAdmin(reversion.VersionAdmin):
    pass


class ActionAdmin(reversion.VersionAdmin):
    pass

#Core
admin.site.register(Action, ActionAdmin)
admin.site.register(Menu, MenuAdmin)
admin.site.register(MainMenu, MainMenuAdmin)
admin.site.register(City, CityAdmin)
admin.site.register(State, StateAdmin)
admin.site.register(Package, PackageAdmin)
admin.site.register(Company, CompanyAdmin)
admin.site.register(Contract, ContractAdmin)
admin.site.register(NaturalPerson, NaturalPersonAdmin)
admin.site.register(Activity)

#Gym
admin.site.register(MuscleGroup, MuscleGroupAdmin)
admin.site.register(Exercise, ExerciseAdmin)
admin.site.register(Question, QuestionAdmin)

from django.contrib import auth

admin.site.unregister(Site)
admin.site.unregister(auth.models.Group)
